package com.baidu.infinity.ui.fragment

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.view.OnApplyWindowInsetsListener
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.baidu.infinity.R
import com.baidu.infinity.databinding.FragmentPinLoginBinding
import com.baidu.infinity.ui.base.BaseFragment
import com.baidu.infinity.ui.util.KeyboardAnimation
import com.baidu.infinity.ui.util.LoginRegisterResult
import com.baidu.infinity.ui.util.PasswordType
import com.baidu.infinity.ui.util.WrongType
import com.baidu.infinity.ui.util.delayTask
import com.baidu.infinity.viewmodel.UserViewModel

class PinLoginFragment : BaseFragment<FragmentPinLoginBinding>(){
    private val userViewModel: UserViewModel by activityViewModels()
    @RequiresApi(Build.VERSION_CODES.R)
    override fun initUI(savedInstanceState: Bundle?) {
        super.initUI(savedInstanceState)
        initKeyboardEvent()
        initKeyboardListener()
        userViewModel.loginRegisterResult.observe(viewLifecycleOwner){
            when(it){
                is LoginRegisterResult.Success ->{
                    mBinding.alterView.showMessage("登录成功")
                    delayTask(1000){
                        findNavController().navigate(R.id.action_pinLoginFragment_to_homeFragment)
                    }
                }
                is LoginRegisterResult.Failure -> {
                    when(it.type){
                        WrongType.USER_NOT_FOUND -> {
                            mBinding.alterView.showErroeMessage("用户不存在")
                        }
                        WrongType.WRONG_PASSWORD -> {
                            mBinding.alterView.showErroeMessage("密码错误")
                        }
                        WrongType.USER_LOGINED -> {
                            mBinding.alterView.showMessage("欢迎回来")
                            delayTask(1000){
                                findNavController().navigate(R.id.action_pinLoginFragment_to_homeFragment)
                            }
                        }
                        else -> {

                        }
                    }
                }
                else -> {}
            }
        }
        mBinding.userName.addTextChangeListener {
            changeLoginBtnState()
        }
        mBinding.password.addTextChangeListener {
            changeLoginBtnState()
        }
        mBinding.loginBtn.setOnClickListener{
            userViewModel.login(mBinding.userName.text(),mBinding.password.text(),PasswordType.PIN)
        }
    }
    @RequiresApi(Build.VERSION_CODES.R)
    private fun initKeyboardEvent(){
        mBinding.root.setOnClickListener{
            val insets = ViewCompat.getRootWindowInsets(mBinding.root)
            insets?.also {
                if(insets.isVisible(WindowInsetsCompat.Type.ime())){
                    mBinding.root.windowInsetsController?.hide(WindowInsetsCompat.Type.ime())
                }
            }
        }
    }
    private fun initKeyboardListener(){
        ViewCompat.setOnApplyWindowInsetsListener(mBinding.userLoginInput,object :
            OnApplyWindowInsetsListener{
            override fun onApplyWindowInsets(
                v: View,
                insets: WindowInsetsCompat
            ): WindowInsetsCompat {
                KeyboardAnimation.setupKeyboardAnimation(mBinding.userLoginInput,120)
                return insets
            }

        })
    }
    private fun changeLoginBtnState(){
        if(mBinding.userName.text().isNotEmpty() && mBinding.password.text().isNotEmpty()){
            mBinding.loginBtn.isEnabled = true
        }else if(mBinding.userName.text().isEmpty() || mBinding.password.text().isEmpty()){
            mBinding.loginBtn.isEnabled = false
        }
    }
}